package cn.tedu.csmall.stock.webapi.quartz.config;

import org.springframework.amqp.core.Binding;
import org.springframework.amqp.core.BindingBuilder;
import org.springframework.amqp.core.DirectExchange;
import org.springframework.amqp.core.Queue;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

// 当前配置类配置RabbitMQ中 交换机\路由Key\队列,以及它们的绑定关系
// 因为它们的关系要保存到Spring容器管理,所以这个类要添加@Configuration注解
@Configuration
public class RabbitMQConfig {

    // 将业务中需要的交换机\路由Key\队列的名称声明为常量
    public static final String STOCK_EX="stock_ex";
    public static final String STOCK_ROUT="stock_rout";
    public static final String STOCK_QUEUE="stock_queue";

    // 交换机实体对象,保存到Spring容器
    @Bean
    public DirectExchange stockDirectExchange(){
        return new DirectExchange(STOCK_EX);
    }
    // 队列实体对象,保存到Spring容器
    @Bean
    public Queue stockQueue(){
        return new Queue(STOCK_QUEUE);
    }
    // 路由Key不是实体,是交换机和队列对象的绑定关系,所以编写比较特殊
    @Bean
    public Binding stockBinding(){
        return BindingBuilder
                .bind(stockQueue()).to(stockDirectExchange()).with(STOCK_ROUT);
    }


}
